#import "ZJMineThroughRecordMoreDraw.h"

@interface ZJMineThroughRecordMoreDraw ()

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIView *contentView;

// 容器视图
@property (nonatomic, strong) UIView *passageTypeView;
@property (nonatomic, strong) UIView *toolsGoodsView;
@property (nonatomic, strong) UIView *statusView;
@property (nonatomic, strong) UIView *timeView;

// 通行类型按钮
@property (nonatomic, strong) UIButton *inButton;
@property (nonatomic, strong) UIButton *outButton;

// 工具货物按钮
@property (nonatomic, strong) UIButton *toolButton;
@property (nonatomic, strong) UIButton *goodsButton;
@property (nonatomic, strong) UIButton *dangerButton;

// 通行状态按钮
@property (nonatomic, strong) UIButton *normalButton;
@property (nonatomic, strong) UIButton *abnormalButton;

// 时间选择
@property (nonatomic, strong) UIButton *startTimeButton;
@property (nonatomic, strong) UIButton *endTimeButton;

// 底部按钮
@property (nonatomic, strong) UIButton *returnButton;
@property (nonatomic, strong) UIButton *confirmButton;

// 添加以下属性来记录当前选中的按钮
@property (nonatomic, strong) UIButton *currentPassageTypeButton;
@property (nonatomic, strong) UIButton *currentToolsGoodsButton;
@property (nonatomic, strong) UIButton *currentStatusButton;

@end

@implementation ZJMineThroughRecordMoreDraw

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
//        [self showDrawerAnimation];
    }
    return self;
}

- (void)setupUI {
    // 半透明黑色背景
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
    
    // 内容视图
    self.contentView = [[UIView alloc] init];
    self.contentView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.contentView];
    
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.right.equalTo(self);
        make.width.equalTo(self.mas_width).multipliedBy(0.8);
    }];
    

//    
//    // 添加具体内容
    [self setupTitleView];
    // 添加各个区域容器
    [self setupContainerViews];
    
    [self setupPassageTypeView];
    [self setupToolsGoodsView];
    [self setupPassageStatusView];
    [self setupTimeSelectionView];
    [self setupBottomButtons];
    //
   
}

- (void)setupContainerViews {
    // 通行类型容器
    self.passageTypeView = [[UIView alloc] init];
    [self.contentView addSubview:self.passageTypeView];
    
    // 工具货物容器
    self.toolsGoodsView = [[UIView alloc] init];
    [self.contentView addSubview:self.toolsGoodsView];
    
    // 通行状态容器
    self.statusView = [[UIView alloc] init];
    [self.contentView addSubview:self.statusView];
    
    // 时间选择容器
    self.timeView = [[UIView alloc] init];
    [self.contentView addSubview:self.timeView];
    
    // 布局各个容器
    [self.passageTypeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(20);
        make.left.right.equalTo(self.contentView).inset(15);
        make.height.mas_equalTo(90);
    }];
    
    [self.toolsGoodsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.passageTypeView.mas_bottom).offset(20);
        make.left.right.equalTo(self.contentView).inset(15);
        make.height.mas_equalTo(90);
    }];
    
    [self.statusView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.toolsGoodsView.mas_bottom).offset(20);
        make.left.right.equalTo(self.contentView).inset(15);
        make.height.mas_equalTo(90);
    }];
    
    [self.timeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.statusView.mas_bottom).offset(20);
        make.left.right.equalTo(self.contentView).inset(15);
        make.height.mas_equalTo(120);
    }];
}

- (void)setupTitleView {
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.text = @"更多查询";
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
    [self.contentView addSubview:self.titleLabel];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(30);
        make.centerX.equalTo(self.contentView);
        make.height.mas_equalTo(20);
    }];
}

- (void)setupPassageTypeView {
    UILabel *typeLabel = [[UILabel alloc] init];
    typeLabel.text = @"通行类型";
    typeLabel.font = [UIFont systemFontOfSize:14];
    [self.passageTypeView addSubview:typeLabel];
    
    self.inButton = [self createFilterButton:@"进"];
    self.outButton = [self createFilterButton:@"出"];
    [self.passageTypeView addSubview:self.inButton];
    [self.passageTypeView addSubview:self.outButton];
    
    [typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.passageTypeView);
        make.height.mas_equalTo(20);
    }];
    
    [self.inButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.passageTypeView);
        make.top.equalTo(typeLabel.mas_bottom).offset(10);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(35);
    }];
    
    [self.outButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.inButton.mas_right).offset(10);
        make.top.equalTo(self.inButton);
        make.width.height.equalTo(self.inButton);
    }];
}

- (void)setupToolsGoodsView {
    UILabel *toolsLabel = [[UILabel alloc] init];
    toolsLabel.text = @"工具货物";
    toolsLabel.font = [UIFont systemFontOfSize:14];
    [self.toolsGoodsView addSubview:toolsLabel];
    
    self.toolButton = [self createFilterButton:@"工具"];
    self.goodsButton = [self createFilterButton:@"货物"];
    self.dangerButton = [self createFilterButton:@"危险品"];
    
    
    [self.toolsGoodsView addSubview:self.toolButton];
    [self.toolsGoodsView addSubview:self.goodsButton];
    [self.toolsGoodsView addSubview:self.dangerButton];
    
    [toolsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.toolsGoodsView);
        make.height.mas_equalTo(20);
    }];
    
    [self.toolButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.toolsGoodsView);
        make.top.equalTo(toolsLabel.mas_bottom).offset(10);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(35);
    }];
    
    [self.goodsButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.toolButton.mas_right).offset(10);
        make.top.width.height.equalTo(self.toolButton);
    }];
    
    [self.dangerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.goodsButton.mas_right).offset(10);
        make.top.width.height.equalTo(self.toolButton);
    }];
}

- (void)setupPassageStatusView {
    UILabel *statusLabel = [[UILabel alloc] init];
    statusLabel.text = @"通行状态";
    statusLabel.font = [UIFont systemFontOfSize:14];
    [self.statusView addSubview:statusLabel];
    
    self.normalButton = [self createFilterButton:@"正常"];
    self.abnormalButton = [self createFilterButton:@"异常"];
    
    [self.statusView addSubview:self.normalButton];
    [self.statusView addSubview:self.abnormalButton];
    
    [statusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.statusView);
        make.height.mas_equalTo(20);
    }];
    
    [self.normalButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.statusView);
        make.top.equalTo(statusLabel.mas_bottom).offset(10);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(35);
    }];
    
    [self.abnormalButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.normalButton.mas_right).offset(10);
        make.top.width.height.equalTo(self.normalButton);
    }];
}

- (void)setupTimeSelectionView {
    UILabel *timeLabel = [[UILabel alloc] init];
    timeLabel.text = @"选择时间";
    timeLabel.font = [UIFont systemFontOfSize:14];
    [self.timeView addSubview:timeLabel];
    
    self.startTimeButton = [self createTimeButton:@"选择开始时间"];
    self.endTimeButton = [self createTimeButton:@"选择结束时间"];
    
    [self.timeView addSubview:self.startTimeButton];
    [self.timeView addSubview:self.endTimeButton];
    
    [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.timeView);
        make.height.mas_equalTo(20);
    }];
    
    [self.startTimeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.timeView);
        make.top.equalTo(timeLabel.mas_bottom).offset(10);
        make.right.equalTo(self.timeView);
        make.height.mas_equalTo(35);
    }];
    
    [self.endTimeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.startTimeButton);
        make.top.equalTo(self.startTimeButton.mas_bottom).offset(10);
        make.height.equalTo(self.startTimeButton);
    }];
}

- (void)setupBottomButtons {
    self.returnButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.returnButton setTitle:@"返回" forState:UIControlStateNormal];
    [self.returnButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    self.returnButton.layer.borderColor = [UIColor blueColor].CGColor;
    self.returnButton.layer.borderWidth = 1;
    self.returnButton.layer.cornerRadius = 4;
    [self.contentView addSubview:self.returnButton];
    
    self.confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.confirmButton setTitle:@"确定" forState:UIControlStateNormal];
    [self.confirmButton setBackgroundColor:[UIColor blueColor]];
    self.confirmButton.layer.cornerRadius = 4;
    [self.contentView addSubview:self.confirmButton];
    
    [self.returnButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(15);
        make.bottom.equalTo(self.contentView).offset(-30);
        make.height.mas_equalTo(44);
        make.width.equalTo(self.confirmButton);
    }];
    
    [self.confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-15);
        make.bottom.height.equalTo(self.returnButton);
        make.left.equalTo(self.returnButton.mas_right).offset(15);
    }];
    
    [self.returnButton addTarget:self action:@selector(returnButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [self.confirmButton addTarget:self action:@selector(confirmButtonClicked) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - Helper Methods

- (UIButton *)createFilterButton:(NSString *)title {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
    button.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1.0];
    button.layer.cornerRadius = 4;
    [button addTarget:self action:@selector(filterButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    return button;
}

- (UIButton *)createTimeButton:(NSString *)title {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1.0];
    button.layer.cornerRadius = 4;
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    button.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    [button addTarget:self action:@selector(timeButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    return button;
}

#pragma mark - Button Actions

- (void)filterButtonClicked:(UIButton *)sender {
    // 根据按钮所属的不同区域进行单选处理
    if ([sender isEqual:self.inButton] || [sender isEqual:self.outButton]) {
        // 通行类型按钮
        if (self.currentPassageTypeButton == sender) {
            // 如果点击当前选中的按钮，取消选中
            sender.selected = NO;
            sender.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1.0];
            self.currentPassageTypeButton = nil;
        } else {
            // 取消之前选中的按钮
            self.currentPassageTypeButton.selected = NO;
            self.currentPassageTypeButton.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1.0];
            // 选中新按钮
            sender.selected = YES;
            sender.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0];
            self.currentPassageTypeButton = sender;
        }
    } else if ([sender isEqual:self.toolButton] || [sender isEqual:self.goodsButton] || [sender isEqual:self.dangerButton]) {
        // 工具货物按钮
        if (self.currentToolsGoodsButton == sender) {
            sender.selected = NO;
            sender.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1.0];
            self.currentToolsGoodsButton = nil;
        } else {
            self.currentToolsGoodsButton.selected = NO;
            self.currentToolsGoodsButton.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1.0];
            sender.selected = YES;
            sender.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0];
            sender.selected = !sender.selected;
            sender.backgroundColor = sender.selected ? [UIColor colorWithWhite:0.9 alpha:1.0] : [UIColor colorWithWhite:0.95 alpha:1.0];
        }
    }
}

- (void)timeButtonClicked:(UIButton *)sender {
    // 实现时间选择逻辑
}

#pragma mark - Animation Methods

- (void)showDrawerAnimation {
    [UIView animateWithDuration:0.25 animations:^{
        CGRect frame = self.contentView.frame;
        frame.origin.x = self.frame.size.width - frame.size.width;
        self.contentView.frame = frame;
    }];
}

- (void)hideDrawerAnimation {
    [UIView animateWithDuration:0.25 animations:^{
        CGRect frame = self.contentView.frame;
        frame.origin.x = self.frame.size.width;
        self.contentView.frame = frame;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

#pragma mark - Touch Events

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    CGPoint location = [[touches anyObject] locationInView:self];
    if (!CGRectContainsPoint(self.contentView.frame, location)) {
        [self hideDrawerAnimation];
    }
}

- (void)returnButtonClicked {
    if ([self.delegate respondsToSelector:@selector(moreDrawDidCancel)]) {
        [self.delegate moreDrawDidCancel];
    }
    [self hideDrawerAnimation];
}

- (void)confirmButtonClicked {
    // 收集所有选中的筛选条件
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    // 通行类型
    if (self.inButton.selected) {
        params[@"passageType"] = @"进";
    }
    if (self.outButton.selected) {
        params[@"passageType"] = @"出";
    }
    
    // 工具货物
    if (self.toolButton.selected) {
        params[@"tool"] = @(YES);
    }
    if (self.goodsButton.selected) {
        params[@"goods"] = @(YES);
    }
    if (self.dangerButton.selected) {
        params[@"danger"] = @(YES);
    }
    
    // 通行状态
    if (self.normalButton.selected) {
        params[@"status"] = @"正常";
    }
    if (self.abnormalButton.selected) {
        params[@"status"] = @"异常";
    }
    
    if ([self.delegate respondsToSelector:@selector(moreDrawDidConfirmWithParams:)]) {
        [self.delegate moreDrawDidConfirmWithParams:params];
    }
    [self hideDrawerAnimation];
}

@end
